/***************************************************************************//**
 * @file
 * @brief single blink handler
 ******************************************************************************/
#include "main_ui.h"

#define SINGLEBLINK_EC                  emberAfPluginLedBlinkLed0EventFunctionEventControl


#define PRIORITY_STARTUP                0
#define PRIORITY_NWKSTATUS              1
#define PRIORITY_STEERING               2
#define PRIORITY_SCENERECALL            3
#define PRIORITY_NONWK                  PRIORITY_SCENERECALL
#define PRIORITY_MODECONF 				4

extern EmberEventControl SINGLEBLINK_EC;

static uint8_t priority = 0xFF;

int singleBlinkCtl_Startup(void){

    halLedBlinkSetActivityLed(BOARDLED0);

    priority = PRIORITY_STARTUP;

    uint16_t ptn[] = {10000};
    halLedBlinkLedOn(0);
    halLedBlinkPattern(100, COUNTOF(ptn), ptn);
    return 0;
}

void singleBlinkCtl_SysInitDone(void){

    //halLedBlinkLedOff(0);
	halLedBlinkLedOff(0);
	//halLedBlinkLedOff(1);
}

// Special
int singleBlinkCtl_Ptn_ModeConfStart(void){

    uint16_t ptn[] = {100, 100, 100, 100, 100, 500, 100, 100, 100, 100, 100, 500}; // 2s

    if(emberEventControlGetActive(SINGLEBLINK_EC)){
    	return -1;
    }

    priority = PRIORITY_MODECONF;
    halLedBlinkPattern(254, COUNTOF(ptn), ptn);
    return 0;
}

int singleBlinkCtl_Ptn_ModeConfStop(void){

    if(emberEventControlGetActive(SINGLEBLINK_EC) && priority == PRIORITY_MODECONF){
    	halLedBlinkLedOff(0);
    	return 0;
    }

    return -1;
}

int singleBlinkCtl_Ptn_NoNwk(void){

    uint16_t ptn[] = {125, 125, 125, 125};

    if(PRIORITY_NONWK < priority || (!emberEventControlGetActive(SINGLEBLINK_EC))){
        priority = PRIORITY_NONWK;
        halLedBlinkPattern(4, COUNTOF(ptn), ptn);
        return 0;
    }else{
        return -1;
    }
}

int singleBlinkCtl_Ptn_NwkUp(void){

    uint16_t ptn[] = {250, 250};

    if(PRIORITY_NWKSTATUS < priority || (!emberEventControlGetActive(SINGLEBLINK_EC))){
        priority = PRIORITY_NWKSTATUS;
        halLedBlinkPattern(4, COUNTOF(ptn), ptn);
        return 0;
    }else{
        return -1;
    }
}

int singleBlinkCtl_Ptn_NwkDown(void){

    return singleBlinkCtl_Ptn_NwkUp();
}

int singleBlinkCtl_Ptn_sceneRecall(void){

    uint16_t ptn[] = {250, 250};

    if(PRIORITY_SCENERECALL < priority || (!emberEventControlGetActive(SINGLEBLINK_EC))){
        priority = PRIORITY_SCENERECALL;
        halLedBlinkPattern(4, COUNTOF(ptn), ptn);
        return 0;
    }else{
        return -1;
    }
}

int singleBlinkCtl_Ptn_SteerTimeout(void){

    uint16_t ptn[] = {500, 500};

    if(PRIORITY_NWKSTATUS < priority || (!emberEventControlGetActive(SINGLEBLINK_EC))){
        priority = PRIORITY_NWKSTATUS;
        halLedBlinkPattern(10, COUNTOF(ptn), ptn);
        return 0;
    }else{
        return -1;
    }
}

int singleBlinkCtl_Ptn_Steering(void){

    uint16_t ptn[] ={125, 125, 125, 125, 125, 125, 125, 125, 
                    125, 125, 125, 125, 125, 125, 125, 125};

    if(PRIORITY_STEERING < priority || (!emberEventControlGetActive(SINGLEBLINK_EC))){
        priority = PRIORITY_STEERING;
        halLedBlinkLedOff(0);
        halLedBlinkPattern(254, COUNTOF(ptn), ptn);
        return 0;
    }else{
        return -1;
    }
}




